package com.inforich.carManage.service.impl;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inforich.appManage.domain.AppUser;
import com.inforich.appManage.domain.Contact;
import com.inforich.appManage.service.IAppRoleService;
import com.inforich.appManage.service.IAppUserService;
import com.inforich.appManage.service.IContactService;
import com.inforich.common.annotation.Excel;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.orderManage.domain.BookingDriverDrivers;
import com.inforich.orderManage.domain.Transport;
import com.inforich.orderManage.service.IBookingDriverDriversService;
import com.inforich.orderManage.service.ITransportService;
import java.util.List;
import java.util.Map;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.carManage.mapper.DriverMapper;
import com.inforich.carManage.domain.Driver;
import com.inforich.carManage.service.IDriverService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 司机Service业务层处理
 *
 * @author hjm
 * @date 2022-10-24
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {

  @Autowired
  private ITransportService transportService;
  @Autowired
  private IBookingDriverDriversService bookingDriverDriversService;
  @Autowired
  private IContactService contactService;
  @Autowired
  private IAppUserService appUserService;
  @Autowired
  private IAppRoleService appRoleService;

  @Override
  public Page<Driver> pageDriver(Page<Driver> page, Map<String, Object> map) {
    return baseMapper.pageDriver(page, map);
  }

  @Override
  public List<Driver> listDriver(Map<String, Object> map) {
    return baseMapper.listDriver(map);
  }

  @Override
  public String checkData(Driver driver) {
    if (!IdcardUtil.isValidCard(driver.getIdnumber())) {
      return "身份证不合法";
    }
    int count = this.count(
        new LambdaQueryWrapper<Driver>().eq(Driver::getCarNumber, driver.getCarNumber())
            .ne(driver.getId() != null, Driver::getId, driver.getId()));
    if (count > 0) {
      return "车牌号重复或已被他人使用";
    }
    int countP = this.count(
        new LambdaQueryWrapper<Driver>().eq(Driver::getPhone, driver.getPhone())
            .ne(driver.getId() != null, Driver::getId, driver.getId()));
    if (countP > 0) {
      return "手机号重复或已被他人使用";
    }
    int countC = this.count(
        new LambdaQueryWrapper<Driver>().eq(Driver::getIdnumber, driver.getIdnumber())
            .ne(driver.getId() != null, Driver::getId, driver.getId()));
    if (countC > 0) {
      return "身份证重复或已被他人使用";
    }
    if (driver.getId() != null) {
      // 修改
      Contact contact = contactService.getOne(new LambdaQueryWrapper<Contact>()
          .eq(Contact::getDriverId, driver.getId()));
      if (contact != null) {
        int countD = appUserService.count(new LambdaQueryWrapper<AppUser>()
            .eq(AppUser::getUserIdnumber, driver.getIdnumber())
            .ne(AppUser::getId, contact.getAppUserId()));
        if (countD > 0) {
          return "该身份信息已被其他用户使用,不可进行此操作";
        }
      }
    } else {
      // 新增
      AppUser appUser = appUserService.getOne(new LambdaQueryWrapper<AppUser>()
          .eq(AppUser::getUserIdnumber, driver.getIdnumber()));
      if (appUser != null) {
        return "该身份信息已被用户" + appUser.getUserName() + "使用,不可进行此操作";
      }
    }
    return null;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public AjaxResult addDriver(Driver driver) {
    return AjaxResult.success(this.save(driver));
  }

  @Override
  public AjaxResult updateDriver(Driver driver) {
    this.updateById(driver);
    Contact contact = contactService.getOne(new LambdaQueryWrapper<Contact>()
        .eq(Contact::getDriverId, driver.getId()));
    if (contact != null) {
      contact.setContactName(driver.getName());
      contact.setContactPhone(driver.getPhone());
      contact.setTransportCompanyId(driver.getTransportCompanyId());
      contactService.updateById(contact);

      appUserService.update(new LambdaUpdateWrapper<AppUser>()
          .set(AppUser::getUserAccount, driver.getIdnumber())
          .set(AppUser::getUserName, driver.getName())
          .set(AppUser::getUserPhone, driver.getPhone())
          .set(AppUser::getUserIdnumber, driver.getIdnumber())
          .eq(AppUser::getId, contact.getAppUserId()));
    }
    return AjaxResult.success();
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public AjaxResult deleteDriver(Long id) {
    int count = transportService.count(new LambdaQueryWrapper<Transport>()
        .eq(Transport::getDriverId, id));
    if (count > 0) {
      return AjaxResult.error("该司机已分配运输单，不可删除");
    }
    this.removeById(id);
    bookingDriverDriversService.remove(new LambdaQueryWrapper<BookingDriverDrivers>()
        .eq(BookingDriverDrivers::getDriverId, id));
    contactService.remove(new LambdaQueryWrapper<Contact>()
        .eq(Contact::getDriverId, id));
    return AjaxResult.success();
  }

  @Override
  public void removeByCompanyId(String companyId) {
    remove(new LambdaQueryWrapper<Driver>()
        .eq(Driver::getTransportCompanyId, companyId));
  }

  @Override
  public Boolean hasTransport(Long driverId) {
    int count = transportService.count(new LambdaQueryWrapper<Transport>()
        .eq(Transport::getDriverId, driverId));
    return count > 0;
  }


}
